﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GandA.Component.Log;
using SACM.Base;
using SACM.Common.Constantes;

namespace SACM.PageConsultorio
{
    public partial class Doctor : BasePage
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            IdItem = 1;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            toolbar.BtnExportar.Click += new EventHandler(BtnExportar_Click);
            toolbar.BtnAgregar.Attributes.Add("onclick", "return OpenRegistro();");
            if (!Page.IsPostBack)
            {
                Master.TituloPagina = "Doctores";
            }
        }
        
        protected void BtnExportar_Click(object sender, EventArgs e)
        {
            if (gridDocs.Rows.Count > 0)
            {
                gridDocs.AllowPaging = false;
                CargaGrid();
                gridDocs.ToExportExcel("Doctores");
                gridDocs.AllowPaging = true;
                CargaGrid();
            }
        }

        protected void gridDocs_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                //string[] sParams;
                //switch (e.CommandName)
                //{
                //    case "cmdModificar":
                //        sParams = e.CommandArgument.ToString().Split(',');
                //        SaveFilters();
                //        Response.Redirect(ResolveUrl("CatDoctoresRegistro.aspx?IdDoctor=" + sParams[0]), false);
                //        break;
                //}
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void gridDocs_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gridDocs.PageIndex = e.NewPageIndex;
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void btnElimina_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Common.Util.Convert.ToNInt(hdnId.Value).GetValueOrDefault(-1);
                EliminaDoctor(id);
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void BtnBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                CargaGrid();
            }
            catch(Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        #region Metodos
        private void EliminaDoctor(int? idDoctor)
        {
            try
            {
                DoctorServiceReference.DoctorServiceClient servicio = new DoctorServiceReference.DoctorServiceClient();
                DoctorServiceReference.Doctor obj = new DoctorServiceReference.Doctor();
                obj.IdDoctor = idDoctor;
                obj.IdEmpresa = SessionEmpresa.IdEmpresa;
                obj.IdSucursal = SessionEmpresa.IdSucursal;
                obj.IdUsuarioMod = SessionUsuario.IdUsuario;
                obj.IpMod = Request.UserHostAddress;
                servicio.Delete(obj);
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        private void CargaGrid()
        {
            ArrayList parametros = new ArrayList();
            ConsultaServiceReference.ConsultaServiceClient servicio = new ConsultaServiceReference.ConsultaServiceClient();
            //Parametros
            parametros.Add(CreaParametroConsulta("@psNombre", TxtNombre.Text, ConsultaServiceReference.Parameter.vEnumType.VarChar));
            parametros.Add(CreaParametroConsulta("@pnIdDoctor", null, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdEmpresa", SessionEmpresa.IdEmpresa, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdSucursal", SessionEmpresa.IdSucursal, ConsultaServiceReference.Parameter.vEnumType.Int));
            ConsultaServiceReference.Parameter[] prms = new ConsultaServiceReference.Parameter[parametros.Count];
            parametros.CopyTo(prms);
            //Ejecuta conulta
            gridDocs.DataSource = servicio.EjecutaConsultaConParametros(General.SACM + ".spObtenDoctores", prms);
            gridDocs.DataBind();
        }
        #endregion
    }
}